var app = angular.module('myApp',[]);
//自定义一个获取数据的service
app.service('bookList',['$http',function($http){
    var createList = {};
    return {
        createList: function () {
            return $http({
                method: 'get',
                url: 'data/books.json'
            }).then(function(res){
                return res.data;
            });
        }
    }
}]);

//左侧菜单部分controller
app.controller('leftMenu',['$rootScope','$scope','$http','bookList',function($rootScope,$scope,$http,bookList){
    $http({
        method:'get',
        url:'data/leftMenu.json'
    }).then(function(res){
        //console.log(res);
        $scope.list = res.data;
    });
    $scope.selectMenu = function(sortId){
        var bookSortList = [];
        bookList.createList().then(function(data){
            for(var i=0;i<data.length;i++){
                if(sortId == 1){  //全部书籍
                    bookSortList = data;
                }else if(sortId == data[i].tag){  //其它分类
                    bookSortList.push(data[i]);
                }
            }
            $rootScope.$broadcast('changeList',bookSortList);
        });
    }
}]);

//右侧内容controller
app.controller('rightCon',['$scope','bookList',function($scope,bookList){
    bookList.createList().then(function(data){
        console.log(data);
       $scope.items = data;
    });
    $scope.$on('changeList',function(e,data){
        $scope.items = data;
    });
}]);

